﻿package com.toncen.samepms.system.action;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.toncen.samepms.common.AbstractDatabaseAction;
import com.toncen.samepms.system.dao.ModuleConfigDao;
import com.toncen.samepms.system.dao.RoleBlockRightDao;
import com.toncen.samepms.system.domain.BlockRight;
import com.toncen.samepms.system.domain.RoleBlockRight;

/**
 * 描述：系统角色模块权限分配表表SYSTEM_ROLE_BLOCK_RIGHT的action文件
 * 
 * @author tch
 * 
 */
public class RoleBlockRightAction  extends AbstractDatabaseAction<RoleBlockRight, RoleBlockRightDao>{
	/**
	 * 序列号
	 */
	private static final long serialVersionUID = 1338535533843L;
	
	private HashMap<String, Object> parentNameMap = new HashMap<String, Object>();
	
	private long roleId;
	
	private List<BlockRight> blockRightList = new ArrayList<BlockRight>();
	

	public HashMap<String, Object> getParentNameMap() {
		return parentNameMap;
	}

	public void setParentNameMap(HashMap<String, Object> parentNameMap) {
		this.parentNameMap = parentNameMap;
	}

	public RoleBlockRightAction() throws Exception{
		super();
	}
	
	/**
	 * create:xl
	 * create date:2012-6-7上午11:41:29
	 * 方法描述：
	 * 保存操作权限
	 * @throws Exception 
	 */
	public String onDoSave() throws Exception
	{
		RoleBlockRightDao dao = new RoleBlockRightDao();
		HttpServletRequest request = ServletActionContext.getRequest();
		RoleBlockRight domian = getDomain();
		dao.batchSaveBlockRight(request,domian.getRoleId());
		return SUCCESS;
	}
	/**
	 * create:xl
	 * create date:2012-6-8上午09:03:56
	 * 方法描述：
	 * 返回模块权限操作页面
	 */
	@SuppressWarnings("unchecked")
	public String setBlockDetailPage()throws Exception
    {
		
		ModuleConfigDao dao = new ModuleConfigDao();
		List list = (List)dao.getAllBlockRight();
		
		RoleBlockRight roleBlockRight = getDomain();
		HttpServletRequest request = ServletActionContext.getRequest();
		roleId = Long.parseLong(request.getParameter("roleId"));
        roleBlockRight.setRoleId(roleId);
        setDomain(roleBlockRight);
        
        HashMap<String, Object> map = new HashMap<String, Object>();
		Iterator it = list.iterator();
		String blockName = "";//模块名称
		String blockNum = "";//模块编码
		String parentName = ""; //模块父节点名称 
		List itemlist = null;//循环类变量
		Iterator itemIt = null;//循环类变量
		HashMap<String, Object> itemMap = new HashMap<String, Object>();//循环类变量
		while (it.hasNext()) {
			BlockRight blockRight = new BlockRight();
			map = (HashMap<String, Object>) it.next();
			
			blockName = String.valueOf(map.get("NAME"));
			blockNum = String.valueOf(map.get("DOMAIN_NAME"));
			parentName = String.valueOf(map.get("PARENTNAME"));
			blockRight.setBlockName(blockName);//获取模块名称
			blockRight.setBlockNum(blockNum);//获取模块编码
			blockRight.setParentName(parentName);
			//行转列，多列保存为一个对象
			RoleBlockRightDao roleBlockRightDao = new RoleBlockRightDao();
			HashMap hashMap = new HashMap();
			
			hashMap.put("roleId", roleId);
			hashMap.put("blockNum", blockNum);
			itemlist =  roleBlockRightDao.getByRoleIdAndBlockNum(hashMap);
			itemIt = itemlist.iterator();
			while(itemIt.hasNext())
			{
				itemMap = (HashMap<String, Object>) itemIt.next();
				blockRight = resetBlockRight(itemMap, blockRight);//重新set对象值
			}
			
			blockRightList.add(blockRight);//将对象放在list中
		}
//		xl  2012-06-1 将模块分组查询放到hashmap中
        groupModuleQuery(dao);
       
        return SUCCESS;
    }

	/**
	 * create:xl
	 * create date:2012-6-10下午02:33:55
	 * 方法描述：
	 * 分组查询模块配置，将查询的结果放到HashMap中
	 */
	@SuppressWarnings("unchecked")
	public void groupModuleQuery(ModuleConfigDao dao) throws Exception {
		HashMap<String, Object> map;
		Iterator it;
		List parentNameList = (List)dao.getAllBlockRightCount();
        map = new HashMap<String, Object>();
        it = parentNameList.iterator();
        while(it.hasNext())
        {
        	map = (HashMap<String, Object>) it.next();
        	parentNameMap.put(String.valueOf(map.get("PARENTNAME")), map.get("COUNTS"));
        }
	}


	/**
	 * create:xl
	 * create date:2012-6-5上午08:52:28
	 * 方法描述：
	 * 重新set对象中的值
	 * @throws NoSuchMethodException 
	 * @throws SecurityException 
	 * @throws InvocationTargetException 
	 * @throws IllegalAccessException 
	 * @throws IllegalArgumentException 
	 */
	@SuppressWarnings("unchecked")
	public BlockRight resetBlockRight(HashMap<String, Object> itemMap, BlockRight blockRight) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
		String rightCode = "";
		rightCode = String.valueOf(itemMap.get("RIGHT_CODE"));
		Class c = blockRight.getClass();
		Field[]  fields = c.getDeclaredFields();//获取对象中的所有属性
		String value  = String.valueOf(itemMap.get("RIGHT_ISOK"));
		for(int i=0;i<fields.length;i++)
		{
			Field field = fields[i];
			String fieldName = fields[i].getName();//属性名称
			String firstLetter = fieldName.substring(0,1).toUpperCase();//属性名首字母大写
			if(rightCode!=null&&(fieldName).equals(rightCode))
			{
				//				获得和属性对应的setXxx()方法的名称
				String setMethodName = "set"+firstLetter+fieldName.substring(1);
				//				getMethod 方法 第一个参数指定一个需要调用的方法名称，第二个参数是需要调用方法的参数类型列表，如没有参数可以指定null，该方法返回一个方法对象
				//				获得和属性对应的setXxx()方法
				Method setMethod = c.getMethod(setMethodName, new Class[]{field.getType()});
				//				动态取得blockRight中属性的值
				Object[] name = {new String(value)};
				//				动态修改blockRight中属性的值
				setMethod.invoke(blockRight, name);
			}
		}
		return blockRight;
	}

	public List<BlockRight> getBlockRightList() {
		return blockRightList;
	}

	public void setBlockRightList(List<BlockRight> blockRightList) {
		this.blockRightList = blockRightList;
	}

	public long getRoleId() {
		return roleId;
	}

	public void setRoleId(long roleId) {
		this.roleId = roleId;
	}


}
